/// Holds and shares the data cross entire app.
/// All data store in a [Map<String, dynamic>] and manipulated by key.
class DataSharing {
  static final Map<String, dynamic> _sharedData = {};

  /// Gets the data with [key].
  static T? get<T>(String key) => _sharedData[key] as T?;

  /// Sets the data with [key] and [value].
  static void set<T>(String key, T? value) {
    if (value == null) {
      _sharedData.remove(key);
    } else {
      _sharedData[key] = value;
    }
  }

  /// Removes the data. [key] used to remove single item. [keys] used to remove multiple items.
  /// If both provided the [keys] takes priority.
  static void remove({List<String>? keys, String? key}) {
    assert(keys != null || key != null);

    final toRemove = keys ?? [key!];

    for (final k in toRemove) {
      _sharedData.remove(k);
    }
  }

  /// Removes all data.
  static void clear() => _sharedData.clear();
}
